package com.jzo2o.market.config;

import com.jzo2o.redis.properties.RedisSyncProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @author Mr.M
 * @version 1.0
 * @description 线程池定义
 * 这里就是教我们怎么去定义线程池
 * @date 2023/12/23 10:35
 */
@Configuration
public class ThreadPoolConfiguration {

    //定义线程池
    @Bean("syncThreadPool")
    public ThreadPoolExecutor synchronizeThreadPool(RedisSyncProperties redisSyncProperties){
        //同步队列的个数，默认10
        int queueNum = redisSyncProperties.getQueueNum();
        //核心 线程数
        int corePoolSize=1;
        //最大线程数，因为我们有10个队列，每个队列由一个线程去处理
        int maximumPoolSize=queueNum;
        //空闲时间
        long keepAliveTime=120;
        //单位设置成秒
        TimeUnit unit = TimeUnit.SECONDS;
        //阻塞队列,该线程当前在忙，如果来执行同步任务，此时不会去在队列存储该任务，使用SynchronousQueue
        BlockingQueue<Runnable> workQueue =   new SynchronousQueue<>();
        //拒绝策略
        // 指定拒绝策略为 DiscardPolicy
        RejectedExecutionHandler rejectedHandler = new ThreadPoolExecutor.DiscardPolicy();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor
                (corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, rejectedHandler);
        return threadPoolExecutor;
    }

}
